<script lang="ts" setup>
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { BpmCategoryApi } from '#/api/bpm/category';

import { Page, useVbenModal } from '@vben/common-ui';

import { ElButton, ElLoading, ElMessage, ElMessageBox } from 'element-plus';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteCategory, getCategoryPage } from '#/api/bpm/category';
import { $t } from '#/locales';

import { useGridColumns, useGridFormSchema } from './data';
import Form from './modules/form.vue';

const [FormModal, formModalApi] = useVbenModal({
  connectedComponent: Form,
  destroyOnClose: true,
});

/** 刷新表格 */
function handleRefresh() {
  gridApi.query();
}

/** 创建流程分类 */
function handleCreate() {
  formModalApi.setData(null).open();
}
/** 复制流程分类 */
function handleCopy(row: BpmCategoryApi.Category) {
  formModalApi.setData(row).open();
}
/** 编辑流程分类 */
function handleEdit(row: BpmCategoryApi.Category) {
  formModalApi.setData(row).open();
}

/** 删除流程分类 */
async function handleDelete(row: BpmCategoryApi.Category) {
  try {
    await ElMessageBox.confirm(
      $t('ui.actionMessage.deleteConfirm', [row.name]),
      $t('common.tips'),
      {
        type: 'warning',
        confirmButtonText: $t('common.confirm'),
        cancelButtonText: $t('common.cancel'),
      },
    );

    const loading = ElLoading.service({
      lock: true,
      text: $t('ui.actionMessage.deleting', [row.name]),
    });

    try {
      await deleteCategory(row.id as number);
      ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
      handleRefresh();
    } finally {
      loading.close();
    }
  } catch {
    // 用户取消删除
  }
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
    // 搜索栏展开折叠按钮是折叠 true 折叠 false 收起
    collapsed: true,
  },
  gridOptions: {
    columns: useGridColumns(onActionClick),
    height: 'auto',
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getCategoryPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
    },
    toolbarConfig: {
      search: true,
      custom: true,
      export: false,
      refresh: true,
      zoom: true,
    },
  } as VxeTableGridOptions<BpmCategoryApi.Category>,
});
/**
 * 表格操作按钮的回调函数
 */
function onActionClick({
  code,
  row,
}: OnActionClickParams<BpmCategoryApi.Category>) {
  switch (code) {
    case 'copy': {
      handleCopy(row);
      break;
    }

    case 'delete': {
      handleDelete(row);
      break;
    }
    case 'edit': {
      handleEdit(row);
      break;
    }
  }
}
</script>

<template>
  <Page auto-content-height>
    <FormModal @success="handleRefresh" />
    <Grid table-title="流程分类">
      <template #toolbar-tools>
        <ElButton type="primary" @click="handleCreate"> 新增 </ElButton>
      </template>
    </Grid>
  </Page>
</template>
